package com.zhl.media.app.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.orhanobut.hawk.Hawk;
import com.zhl.media.app.R;
import com.zhl.media.app.di.component.DaggerAdjustedComponent;
import com.zhl.media.app.di.module.AdjustedModule;
import com.zhl.media.app.event.Event;
import com.zhl.media.app.greendao.MediaPositionDao;
import com.zhl.media.app.greendao.ProjDao;
import com.zhl.media.app.model.MediaPosition;
import com.zhl.media.app.model.Proj;
import com.zhl.media.app.mvp.adapter.AdjustListViewAdapter;
import com.zhl.media.app.mvp.contract.AdjustedContract;
import com.zhl.media.app.mvp.presenter.AdjustedPresenter;
import com.zhl.media.app.utils.EntityManager;
import com.zhl.media.commonres.base.AppActivity;
import com.zhl.media.commonres.dialog.ConfirmDialog;
import com.zhl.media.commonres.view.PublicTitleBar;
import com.zhl.media.commonsdk.core.RouterHub;
import com.zhl.media.commonsdk.entity.AdJustMedia;
import com.zhl.media.commonsdk.enumeration.MediaCode;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

import static com.jess.arms.utils.Preconditions.checkNotNull;
// 待调整列表
@Route(path = RouterHub.APP_ADJUSTEDACTIVITY)
public class AdjustedActivity extends AppActivity<AdjustedPresenter> implements AdjustedContract.View {
    @BindView(R.id.titleBar)
    PublicTitleBar titleBar;

    @BindView(R.id.list_view)
    ListView listView;

    List<AdJustMedia> mData;

    MediaPosition mediaPosition;
    AdJustMedia oldMediaPosition;
    private AdjustListViewAdapter adapter;
    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerAdjustedComponent.builder().appComponent(appComponent).adjustedModule(new AdjustedModule(this)).build().inject(this);
    }
    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_adjusted; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }
    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        titleBar.setTitle("待调整媒体位");
        Intent i = getIntent();
        mediaPosition = (MediaPosition)i.getSerializableExtra("media");
        mData = new ArrayList<>();
        adapter = new AdjustListViewAdapter(this,R.layout.adjust_list_item,mData);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                oldMediaPosition = mData.get(position);
                ConfirmDialog confirmDialog = new ConfirmDialog(AdjustedActivity.this,"调整位置","确认是否调整该媒体位？","取消","确定");
                confirmDialog.show();
                confirmDialog.setListener(new ConfirmDialog.HallListener() {
                    @Override
                    public void confirm() {
                        confirmDialog.dismiss();
                        mPresenter.updateAdjust(mData.get(position).getId(),mediaPosition.getId());
                    }
                });
            }
        });
        mPresenter.selectAdjust();
    }
    @Override
    public void showLoading() {

    }
    @Override
    public void hideLoading() {
    }
    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }
    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }
    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public void onSuccess(List<AdJustMedia> list) {
        mData.clear();
        mData.addAll(list);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onAdjust(Map map) {

        String mediaId = (String)map.get("mediaId");
        String unitId = (String)map.get("unitId");
        String buildingId = (String)map.get("buildingId");
        String projectId = (String)map.get("projectId");


        String oldMediaId = (String)map.get("oldMediaId");
        String oldUnitId = (String)map.get("oldUnitId");
        String oldBuildingId = (String)map.get("oldBuildingId");
        String oldProjectId = (String)map.get("oldProjectId");

        ProjDao projDao = EntityManager.getInstance(this).getProjDao();
        MediaPositionDao mediaPositionDao = EntityManager.getInstance(this).getMediaPositionDao();

        //更新旧位置项目
        Proj oldPro = projDao.queryBuilder().where(ProjDao.Properties.Id.eq(oldProjectId)).unique();
        oldPro.setYiNum(oldPro.getYiNum()+1);
        projDao.update(oldPro);
        //更新旧位置楼座
        Proj oldBuilding = projDao.queryBuilder().where(ProjDao.Properties.Id.eq(oldBuildingId)).unique();
        oldBuilding.setYiNum(oldBuilding.getYiNum()+1);
        projDao.update(oldBuilding);
        //更新旧位置单元
        Proj oldUnit = projDao.queryBuilder().where(ProjDao.Properties.Id.eq(oldUnitId)).unique();
        oldUnit.setYiNum(oldUnit.getYiNum()+1);
        projDao.update(oldUnit);

        //更新旧位置媒体位
        MediaPosition oldMedia = mediaPositionDao.queryBuilder().where(MediaPositionDao.Properties.Id.eq(oldMediaId)).unique();
        oldMedia.setStatus(MediaCode.WCTZ);
        oldMedia.setStu(MediaCode.WCTZ.getKey());
        mediaPositionDao.update(oldMedia);

        //更新新位置
        Proj newPro = projDao.queryBuilder().where(ProjDao.Properties.Id.eq(projectId)).unique();
        newPro.setParentId("0");
        newPro.setYtzNum(newPro.getYtzNum()+1);
        newPro.setSumNum(newPro.getSumNum()+1);
        newPro.setType(1);
        projDao.update(newPro);

        //更新新位置楼座
        Proj building = projDao.queryBuilder().where(ProjDao.Properties.Id.eq(buildingId)).unique();
        building.setSumNum(building.getSumNum()+1);
        projDao.update(building);

        //更新新位置单元
        Proj unit = projDao.queryBuilder().where(ProjDao.Properties.Id.eq(unitId)).unique();
        unit.setSumNum(unit.getSumNum()+1);
        projDao.update(unit);

        MediaPosition newMedia = mediaPositionDao.queryBuilder().where(MediaPositionDao.Properties.Id.eq(mediaId)).unique();
        newMedia.setStu(MediaCode.YTZ.getKey());
        newMedia.setStatus(MediaCode.YTZ);
        newMedia.setAlphabetIdentification(oldMedia.getAlphabetIdentification());
        newMedia.setSchemeId(oldMedia.getSchemeId());
        newMedia.setSchemeName(oldMedia.getSchemeName());
        newMedia.setType(3);
        mediaPositionDao.update(newMedia);

        Event.postUpdateMtw();
        Event.postUpdateGzrwList();
        Event.postUpdateDscNum();
        finish();
    }
}
